<template>
  <el-row>
    <el-col :span="24">
      <el-form class="el-form-col" :model="model" :rules="rules" label-position="left" ref="elForm" label-width="100px">
        <Portlet title="订单信息">
          <el-row :gutter="15">

            <el-col :md="12" :lg="8" :xl="6"  v-if="orderNo">
              <el-form-item label="入库单号">
                {{ orderNo }}
              </el-form-item>
            </el-col>

            <el-col :md="12" :lg="8" :xl="6">
              <el-form-item label="入库仓" prop="warehouseId" :rules="[
                    {required: true, message: '入库仓必须', trigger: 'change'}
                  ]">
                <el-select v-model="model.warehouseId" placeholder="请选择入库仓">
                  <el-option v-for="item in warehouseIdArr" :key="item.value" :label="item.text" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :md="12" :lg="8" :xl="6">
              <el-form-item  label="商家名称" prop="cargoOwnerId" :rules="[
                    {required: true, message: '商家必须', trigger: 'change'}
                  ]">

                <RemoteSelect
                  v-model="model.cargoOwnerId"
                  :config="{
                    text: 'cargoOwnerName',
                    value: 'cargoOwnerId',
                    searchKey: 'cargoOwnerName',
                    url: '/ms-warehouse-order/WhPackage/findCargoOwner'
                  }"
                  :getParams="getParams"
                  :disabled="isEditOrder"
                  :remote="false"
                  :beforeRequest="beforeRequest"
                  ref="cargoOwnerIdSelect"
                  @setOptions="setCargoOwnerOptions"
                  :initOptions="cargoOwnerArr"
                >
                </RemoteSelect>
              </el-form-item>
            </el-col>

            <el-col :md="12" :lg="8" :xl="6">
              <el-form-item label="入库类型" prop="inputTypeCode">
                <span>{{ model.inputTypeCode | filterDataBaseDictionary}}</span>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row class="service-list">
            <el-col :span="24">
              <el-form-item label="服务类型" prop="addedServiceList">
                <el-checkbox-group v-model="model.addedServiceList">
                  <el-checkbox v-for="item in addedService" :label="item.serviceTypeCode" :key="item.serviceTypeCode"  :disabled="item.choosable == 1 || isEditOrder" border>{{ item.serviceType }}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <span class="help-block" v-if="isEditOrder">服务类型不可修改</span>
            </el-col>
          </el-row>
        </Portlet>

        <Portlet title="提货信息" v-if="hasPickUp">
          <el-row :gutter="15" class="row">

            <el-col :md="24" :xl="12">
              <el-form-item label="是否到货" prop="orderPickUpInfo.doArrive" :rules="[
                    {required: true, message: '是否到货必须', trigger: 'blur'}
                  ]">
                <el-switch
                  v-model="model.orderPickUpInfo.doArrive"
                  active-value="1"
                  inactive-value="0"
                  active-text="是"
                  inactive-text="否">
                </el-switch>
              </el-form-item>
            </el-col>

            <el-col :md="24" :xl="12">
              <el-form-item label="预计提货" prop="orderPickUpInfo.predictDevliveryDate">
                <el-date-picker :picker-options="pickerOptions" type="date" placeholder="选择年月日"
                                v-model="model.orderPickUpInfo.predictDevliveryDate"
                                value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :md="24" :xl="12">
              <el-form-item label="提货联系人" prop="orderPickUpInfo.pickUpName" :rules="[
                    {required: true, message: '提货联系人必须', trigger: 'blur'}
                  ]">
                <el-input :maxlength="50"  v-model="model.orderPickUpInfo.pickUpName" />
              </el-form-item>
            </el-col>

            <el-col :md="24" :xl="12">
              <el-form-item label-width="110px" label="提货联系电话" prop="orderPickUpInfo.pickUpPhone" :rules="[
                    {required: true, message: '提货联系电话必须', trigger: 'blur'},
                    {validator: isPhone, message: '电话格式不正确', trigger: 'blur'},
                  ]">
                <el-input :maxlength="50" v-model="model.orderPickUpInfo.pickUpPhone" />
              </el-form-item>
            </el-col>

            <el-col :md="24" :xl="12">
              <el-form-item label="提货地址" prop="orderPickUpInfo.addressArr" :rules="[
                {required: true, message: '提货地址必须', type:'array', trigger: 'change'}
              ]">
                <CascadeAddress v-model="model.orderPickUpInfo.addressArr" :level="3"/>
              </el-form-item>
            </el-col>

            <el-col :md="24" :xl="12">
              <el-form-item label="详细地址"  prop="orderPickUpInfo.pickUpAddress" :rules="[
                {required: true, message: '详细地址必须', trigger: 'blur'}
              ]">
                <el-input type="textarea" :maxlength="200" v-model="model.orderPickUpInfo.pickUpAddress"/>
              </el-form-item>
            </el-col>

            <el-col :md="24" :xl="12">
              <el-form-item label="提货备注" prop="orderPickUpInfo.pickUpMemo">
                <el-input :maxlength="300" type="textarea" v-model="model.orderPickUpInfo.pickUpMemo"/>
              </el-form-item>
            </el-col>
          </el-row>
        </Portlet>
        <Portlet title="商品信息">

          <PackageSelectModal :value="model.productDetailBeans" @addPackage="addPackage" :visible.sync="outerVisible" :cargoOwnerId="model.cargoOwnerId"></PackageSelectModal>
          <el-button slot="tools" type="primary" @click.prevent="showPackageSelectModal">添加商品</el-button>
          <el-row :gutter="15">
            <el-col :span="24">
              <el-form-item label-width="0" prop="productDetailBeans" :rules="[
                {required: true, message: '商品信息必须', trigger: 'change', type: 'array'}
             ]">
                <PackageTable v-model="model.productDetailBeans"></PackageTable>
              </el-form-item>
            </el-col>

            <el-col :md="12" :lg="8" :xl="6">
              <el-form-item label="总包件">
                <span>{{ computedTotalPackage }}</span>
              </el-form-item>
            </el-col>

            <el-col :md="12" :lg="8" :xl="6">
              <el-form-item label="建议体积">
                <span>{{ computedTotalVolume | decimalNumFilter }}m³</span>
              </el-form-item>
            </el-col>

            <el-col :md="12" :lg="8" :xl="6">
              <el-form-item
                prop="totalVolume"
                :rules="[
                  {required: true, message: '总体积必须', trigger: 'blur'},
                  {validator: isFormatNumber, int: 9, message: '总体积必须是整数小于9位小数小于2位的小数', trigger: 'blur'},
                  {validator: range, min: 0.01, message: '总体积必须大于0', trigger: 'blur'}
                ]"
                required
                label="总体积">
                <el-input
                  :maxlength="50"
                  v-model="model.totalVolume">
                  <template slot="append">
                    <el-button @click.prevent="suggestAllVolume">使用建议</el-button>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>

            <el-col :md="12" :lg="8" :xl="6">
              <el-form-item
                prop="totalWeight"
                :rules="[
                  {validator: isFormatNumber, int: 9,  message: '总重量必须是整数小于9位小数小于2位的小数', trigger: 'blur'},
                  {validator: range, min: 0.01,  message: '总重量必须大于0', trigger: 'blur'}
                ]"
                label="总重量">
                <el-input :maxlength="50" v-model="model.totalWeight">
                  <template slot="append">kg</template>
                </el-input>
              </el-form-item>
            </el-col>

          </el-row>

        </Portlet>

        <Portlet title="费用信息" v-if="model.addedServiceList.length > 0">
          <h4 slot="tools" style="font-size:14px">
            服务完成后，应付金额在结算报表中填写。
          </h4>
          <el-row>
            <el-col :span="24">
              <el-table
                border
                max-height="500"
                :data="model.costList"
                class="text-center"
              >
                <el-table-column
                  label="结算主体"
                >
                  <template slot-scope="scope">
                    {{ cargoOwnerName }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="costName"
                  label="费用名称">
                  <template slot-scope="scope">
                    {{ scope.row.costName}}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="money"
                  width="240"
                  label="应收金额">
                  <template slot-scope="scope">
                    <el-form-item label-width="0" :prop="'costList.' + scope.$index + '.money'" :rules="[
                      {validator: isFormatNumber, int: 9, message: '应收金额必须是整数小于9位小数小于2位的小数', trigger: 'blur'}
                    ]">
                      <el-input v-model="scope.row.money" placeholder="0"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
        </Portlet>

        <Portlet>
          <el-row>
            <el-col :span="24" class="text-center">
              <template  v-if="routeName == 'orderCreate' || routeName == 'orderEdit'">
                <el-button :loading="loading" class="btn-block" @click.prevent="submit" type="primary">提交</el-button>
                <el-button :loading="tempLoading" class="btn-block" @click.prevent="saveTemp" type="primary">保存至暂存单</el-button>
              </template>

              <template v-if="routeName == 'orderTempShow'">
                <el-button :loading="loading" class="btn-block" @click.prevent="submit" type="primary">提交</el-button>
                <el-button :loading="saveLoading" class="btn-block" @click.prevent="save" type="primary">保存</el-button>
              </template>

              <template v-if="routeName == 'warehousingOrderEdit'">
                <el-button :loading="loading" class="btn-block" @click.prevent="submit" type="primary">提交</el-button>
              </template>
            </el-col>
          </el-row>
        </Portlet>
      </el-form>
    </el-col>
  </el-row>
</template>
<script>
import PackageSelectModal from '@/views/oms/order/PackageSelectModal'
import PackageTable from '@/views/oms/order/PackageTable'
import {getMyWarehouse, getAllServiceTypeByType} from '@/utils/selectOptions'
import {copy, dotData, alert, confirm, isEqual} from '@/utils'
import {filterDataBaseDictionary, decimalNumFilter} from '@/filters'
import {isFormatNumber, isPhone, range} from '@/utils/validate'
import L from 'lodash'
import moment from 'moment'
export default {
  name: 'orderCreate',
  components: {
    PackageSelectModal,
    PackageTable
  },
  filters: {
    filterDataBaseDictionary
  },
  data() {
    return {
      id: this.$route.params.id,
      routeName: this.$route.name,
      outerVisible: false,
      loading: false,
      saveLoading: false,
      tempLoading: false,
      model: {
        productDetailBeans: [],
        inputTypeCode: 'CARGOSOURCETYPE01',
        warehouseId: '',
        cargoOwnerId: '',
        costList: [],
        addedServiceList: [],
        orderPickUpInfo: {
          pickUpAddress: '',
          addressArr: [],
          pickUpMemo: '',
          pickUpName: '',
          pickUpPhone: '',
          doArrive: 0,
          predictDevliveryDate: ''
        },
        totalVolume: '',
        totalWeight: ''
      },
      pickerOptions: {
        disabledDate(time) {
          return moment(time).isBefore(Date.now(), 'day')
        }
      },
      warehouseIdArr: [],
      rules: {},
      restaurants: [],
      cargoOwnerArr: [],
      timeout: '',
      cargoOwnerLoading: false,
      orderNo: '',
      serviceTypes: [{
        text: '上门提货',
        value: '上门提货'
      }],
      serviceTypeArr: [],
      initLoading: false
    }
  },
  computed: {
    // 选中的服务类型
    checkedServiceType() {
      return this.model.addedServiceList
    },
    checkedServiceTypeCostList() {
      let temp = []
      this.checkedServiceType.forEach(v => {
        let item = this.addedServiceMap.get(v)
        if (item) {
          temp.push(dotData(item, 'costList'))
        }
      })
      return temp
    },
    cargoOwnerName() {
      let id = this.model.cargoOwnerId
      let list = this.cargoOwnerArr
      if (Array.isArray(list)) {
        for (let v of list) {
          if (v.value === id) {
            return v.text
          }
        }
      }
      return id
    },
    // 服务
    serviceList() {
      for (let v of this.serviceTypeArr) {
        if (v.orderNatureCode === 'all') {
          return v.serviceTypeList
        }
      }
      return []
    },
    // 增值费用
    addedService() {
      return this.serviceList.filter(v => {
        if (v.orderNatureCode === 'all') {
          return true
        }
        return false
      })
    },
    // 增值服务映射
    addedServiceMap() {
      let map = new Map()
      if (Array.isArray(this.addedService)) {
        this.addedService.forEach(v => {
          map.set(v.serviceTypeCode, {costList: v.costList})
        })
      }
      return map
    },
    // 有提货人信息
    hasPickUp() {
      return this.checkedServiceType.indexOf('CHADDEDEDSERVICE06') >= 0
    },
    isEditOrder() {
      return this.routeName === 'warehousingOrderEdit'
    },
    // 商家名称需要收货仓编号
    cargoOwnerIdNeedWarehouseId() {
      return ['CARGOSOURCETYPE01'].indexOf(this.model.inputTypeCode) >= 0
    },
    // 总包件数
    computedTotalPackage() {
      return this.model.productDetailBeans.reduce((total, item) => {
        const num = parseInt(item.num)
        if (num) {
          return total + num
        }
        return total
      }, 0)
    },
    // 总体积
    computedTotalVolume() {
      return this.model.productDetailBeans.reduce((total, item) => {
        const totalVolume = parseInt(item.num) * parseFloat(item.volume)
        if (totalVolume) {
          return total + totalVolume
        }
        return total
      }, 0)
    }
  },
  watch: {
    // 设置费用
    checkedServiceTypeCostList(val, oldVal) {
      if (isEqual(val, oldVal)) {
        return
      }
      if (this.initLoading) {
        return
      }
      this.setModelFee(val)
    },
    hasPickUp(val, oldVal) {
      if (val === oldVal) {
        return
      }
      if (this.initLoading) {
        return
      }
      if (val === false) {
        // 清空提货信息
        this.model.orderPickUpInfo = {
          pickUpAddress: '',
          addressArr: [],
          pickUpMemo: '',
          pickUpName: '',
          pickUpPhone: ''
        }
      }
    },
    // 根据入库类型来
    'model.inputTypeCode'(val, oldVal) {
      if (val === oldVal) {
        return
      }
      if (this.initLoading) {
        return
      }
      this.model.warehouseId = ''
      getMyWarehouse(val).then(arr => {
        this.warehouseIdArr = arr
      })
    },
    warehouseIdArr(val, oldVal) {
      // 只有一个时默认选中
      if (Array.isArray(val) && val.length === 1) {
        this.model.warehouseId = val[0].value
      }
    },
    'model.warehouseId'(val, oldVal) {
      if (val === oldVal) {
        return
      }
      if (this.initLoading) {
        return
      }
      this.model.cargoOwnerId = ''
      if (this.$refs.cargoOwnerIdSelect) {
        this.$refs.cargoOwnerIdSelect.clearOptions()
      }
    }
  },
  created() {
    getMyWarehouse(this.model.inputTypeCode).then(arr => {
      this.warehouseIdArr = arr
    })
    getAllServiceTypeByType('put').then(arr => {
      if (Array.isArray(arr)) {
        this.serviceTypeArr = arr
      }
    })
    if (this.id) {
      let url = ''
      if (this.routeName === 'orderTempShow') {
        url = '/ms-warehouse-order/orderTemp/getOrderTempDetail'
      } else if (this.routeName === 'orderEdit') {
        url = '/ms-warehouse-order/recordOrder/orderDetailBean'
      } else if (this.routeName === 'warehousingOrderEdit') {
        url = '/ms-warehouse-order/recordOrder/orderDetailBean'
      }
      if (url) {
        this.initLoading = true
        this.$ajax.get(url, {id: this.id}).then(response => {
          const success = dotData(response, 'success')
          if (success) {
            // 初始 cargoOwnerArr
            if (dotData(response, 'data.chOrderService.cargoOwnerId')) {
              this.cargoOwnerArr = [
                {
                  text: dotData(response, 'data.chOrderService.cargoOwner'),
                  value: dotData(response, 'data.chOrderService.cargoOwnerId')
                }
              ]
            }
            let PickUpInfoAddress = []
            if (dotData(response, 'data.orderPickUpInfo.pickUpProvinceNo')) {
              PickUpInfoAddress.push(dotData(response, 'data.orderPickUpInfo.pickUpProvinceNo'))
              if (dotData(response, 'data.orderPickUpInfo.pickUpCityNo')) {
                PickUpInfoAddress.push(dotData(response, 'data.orderPickUpInfo.pickUpCityNo'))
                if (dotData(response, 'data.orderPickUpInfo.pickUpDistrictNo')) {
                  PickUpInfoAddress.push(dotData(response, 'data.orderPickUpInfo.pickUpDistrictNo'))
                }
              }
            }
            this.model.productDetailBeans = dotData(response, 'data.productDetailBeans') || []
            this.model.warehouseId = dotData(response, 'data.chOrderService.warehouseId')
            this.model.cargoOwnerId = dotData(response, 'data.chOrderService.cargoOwnerId')
            if (dotData(response, 'data.chOrderService.totalVolume')) {
              this.model.totalVolume = dotData(response, 'data.chOrderService.totalVolume')
            }
            this.model.totalWeight = dotData(response, 'data.chOrderService.totalWeight')
            this.model.addedServiceList = dotData(response, 'data.addedServiceList') || []
            this.model.costList = dotData(response, 'data.costList') || []
            this.model.orderPickUpInfo = {
              pickUpAddress: dotData(response, 'data.orderPickUpInfo.pickUpAddress'),
              addressArr: PickUpInfoAddress,
              pickUpMemo: dotData(response, 'data.orderPickUpInfo.pickUpMemo'),
              pickUpName: dotData(response, 'data.orderPickUpInfo.pickUpName'),
              pickUpPhone: dotData(response, 'data.orderPickUpInfo.pickUpPhone'),
              doArrive: dotData(response, 'data.orderPickUpInfo.doArrive') || 0,
              predictDevliveryDate: dotData(response, 'data.orderPickUpInfo.predictDevliveryDate')
            }
            this.orderNo = dotData(response, 'data.chOrderService.orderNo')
          }
        }).finally(_ => {
          this.initLoading = false
        })
      }
    }
  },
  methods: {
    isFormatNumber,
    isPhone,
    range,
    beforeRequest() {
      if (this.cargoOwnerIdNeedWarehouseId && !this.model.warehouseId) {
        alert('请先选择入库仓')
        return false
      }
      return true
    },
    getParams(query) {
      let params = {
        cargoOwnerName: query,
        storageType: this.model.inputTypeCode
      }
      if (this.model.warehouseId) {
        params.warehouseId = this.model.warehouseId
      }
      return params
    },
    remoteMethod: L.debounce((query, _this) => {
      if (!query) {
        _this.cargoOwnerLoading = false
        return
      }
      if (!_this.model.inputTypeCode) {
        alert('请选择入库类型')
        return
      }
      this.$ajax.post('/ms-warehouse-order/WhPackage/findCargoOwner', {
        cargoOwnerName: query,
        storageType: _this.model.inputTypeCode
      }).then(response => {
        const success = dotData(response, 'success')
        if (success) {
          let data = dotData(response, 'data')
          let tempArr = []
          if (Array.isArray(data)) {
            data.forEach((v, k) => {
              tempArr.push({
                text: v.cargoOwnerName,
                value: v.cargoOwnerId
              })
            })
          }
          _this.cargoOwnerArr = tempArr
        }
      }).finally(_ => {
        _this.cargoOwnerLoading = false
      })
    }, 1000),
    remoteQuery() {
      if (!this.model.inputTypeCode) {
        alert('请选择入库类型')
        return
      }
      this.cargoOwnerLoading = true
      // this.remoteMethod(query, this) 改为全部展示
      this.httpGetCargoOwnerAll()
    },
    // 获取所有商家
    httpGetCargoOwnerAll() {
      if (this.cargoOwnerArr.length > 0) {
        this.cargoOwnerLoading = false
        return
      }
      this.$ajax.get('/ms-warehouse-order/WhPackage/findCargoOwner', {storageType: this.model.inputTypeCode}).then(response => {
        const success = dotData(response, 'success')
        if (success) {
          let data = dotData(response, 'data')
          let tempArr = []
          if (Array.isArray(data)) {
            data.forEach((v, k) => {
              tempArr.push({
                text: v.cargoOwnerName,
                value: v.cargoOwnerId
              })
            })
          }
          this.cargoOwnerArr = tempArr
        }
      }).finally(_ => {
        this.cargoOwnerLoading = false
      })
    },
    showPackageSelectModal() {
      if (!this.model.cargoOwnerId) {
        alert('请先选择商家')
        return
      }
      this.outerVisible = true
    },
    // 保存
    save() {
      this.preValidate()
      this.$refs.elForm.validate(result => {
        if (!result) {
          alert('验证未通过')
          return
        }
        this.saveLoading = true
        this.httpPost('/ms-warehouse-order/orderTemp/editOrderTemp').then(response => {
          const success = dotData(response, 'success')
          if (success) {
            // 保存成功
            confirm('保存成功', '提示', {
              type: 'success'
            }).finally(_ => {
              if (this.routeName === 'orderTempShow') {
                this.$router.push({path: '/ss-oms/order/temp'})
              }
              this.saveLoading = false
            })
          } else {
            this.saveLoading = false
          }
        }).catch(_ => {
          this.saveLoading = false
        })
      })
    },
    saveTemp() {
      this.preValidate()
      this.$refs.elForm.validate(result => {
        if (!result) {
          return
        }
        this.tempLoading = true
        this.httpPost('/ms-warehouse-order/recordOrder/saveInputOrderTemp').then(response => {
          const success = dotData(response, 'success')
          if (success) {
            confirm('保存成功', undefined, {type: 'success'}).then(_ => {
              this.$router.push({path: '/ss-oms/order/temp'})
            }).finally(_ => {
              this.resetFields()
              this.tempLoading = false
            })
          } else {
            this.tempLoading = false
          }
        }).catch(_ => {
          this.tempLoading = false
        })
      })
    },
    httpPost(url) {
      let data = copy(this.model, true)
      if (this.routeName === 'orderTempShow') { // 暂存单添加一个标识
        data.mark = 'temp'
      }
      // 产品处理
      let productDetailBeans = []
      if (Array.isArray(data.productDetailBeans)) {
        data.productDetailBeans.forEach(v => {
          productDetailBeans.push({
            packageId: v.packageId,
            num: v.num
          })
        })
      }
      data.productDetailBeans = productDetailBeans
      // 费用处理
      if (Array.isArray(data.costList)) {
        data.costList.forEach(v => {
          if (!v.money) {
            v.money = 0
          }
          v.clearingObject = data.cargoOwnerId
        })
      }
      // 提货信息处理
      if (this.hasPickUp) {
        data.orderPickUpInfo.pickUpProvinceNo = dotData(data, 'orderPickUpInfo.addressArr[0]')
        data.orderPickUpInfo.pickUpCityNo = dotData(data, 'orderPickUpInfo.addressArr[1]')
        data.orderPickUpInfo.pickUpDistrictNo = dotData(data, 'orderPickUpInfo.addressArr[2]')
        delete data.orderPickUpInfo.addressArr
      } else {
        delete data.orderPickUpInfo
      }
      if (this.id) {
        // 暂存单
        if (this.routeName === 'orderTempShow') { // 暂存单添加一个标识
          data.orderId = this.id
        }
        data.id = this.id
      }
      if (!data.totalVolume) {
        alert('总体积必须')
        return
      }
      return this.$ajax.post(url, data).then(response => {
        return response
      })
    },
    // 验证前操作
    preValidate() {
    },
    submit() {
      this.preValidate()
      this.$refs.elForm.validate(result => {
        if (!result) {
          alert('验证未通过')
          return
        }
        let url = ''
        let title = ''
        if (this.id) {
          if (this.routeName === 'orderEdit' || this.routeName === 'warehousingOrderEdit') { // 编辑
            title = '编辑成功'
            url = '/ms-warehouse-order/recordOrder/modifyOrder'
          } else if (this.routeName === 'orderTempShow') {
            title = '转正成功'
            url = '/ms-warehouse-order/recordOrder/record'
          }
        } else {
          title = '新增成功'
          url = '/ms-warehouse-order/recordOrder/record'
        }
        if (!url) {
          alert('submit 地址未取得')
          return
        }
        this.loading = true
        this.httpPost(url).then(response => {
          const success = dotData(response, 'success')
          if (success) {
            confirm(title, title, {
              type: 'success',
              showCancelButton: false,
              closeOnClickModal: false
            }).then(_ => {
              if (this.id) {
                if (this.routeName === 'orderEdit') { // 详情的编辑
                  this.$router.push({path: '/ss-oms/enterWarehouse/warehousing-order/show/' + this.id})
                } else if (this.routeName === 'orderTempShow') { // 暂存单转正
                  this.$router.push({path: '/ss-oms/order/temp'})
                } else if (this.isEditOrder) {
                  this.$router.push({path: '/ss-oms/enterWarehouse/warehousing-order/edit-index'})
                }
              } else {
                this.resetFields()
              }
            }).finally(_ => {
              this.loading = false
            })
          } else {
            this.loading = false
          }
        }).catch(_ => {
          this.loading = false
        })
      })
    },
    // 清空表格
    resetFields() {
      try {
        this.$refs.elForm.resetFields()
      } catch (err) {
        console.log(err)
      }
    },
    // 添加包件
    addPackage(arr) {
      let productDetailBeansMap = new Map()
      this.model.productDetailBeans.forEach(v => {
        productDetailBeansMap.set(v.packageId, v)
      })
      arr.forEach(v => {
        if (!productDetailBeansMap.has(v.packageId)) {
          v.num = ''
          this.model.productDetailBeans.push(v)
        }
      })
    },
    // 根据服务类型设置费用
    setModelFee(list) {
      let typeMap = new Map()
      if (Array.isArray(this.model.costList)) {
        this.model.costList.forEach(v => {
          let code = dotData(v, 'code')
          if (code) {
            typeMap.set(code, v)
          }
        })
      }
      let checkListMap = new Map()
      if (Array.isArray(list)) {
        list.forEach(v => {
          if (Array.isArray(v)) {
            v.forEach(v2 => {
              if (!typeMap.has(v2.code)) { // typeMap里面没有type, 但model里面有这个type（model里面的type要保持一致）
                this.model.costList.push({
                  costName: v2.costName,
                  code: v2.code,
                  serviceNo: v2.serviceNo,
                  money: ''
                })
              }
              checkListMap.set(v2.code, v2)
            })
          } else {
            if (!typeMap.has(v.code)) {
              this.model.costList.push({
                costName: v.costName,
                code: v.code,
                serviceNo: v.serviceNo,
                money: ''
              })
            }
            checkListMap.set(v.code, v)
          }
        })
      }
      // 如果 model里面有的type，但typeMap里面无的type, 那就要把它过滤掉
      this.model.costList = this.model.costList.filter(v => {
        let code = dotData(v, 'code')
        return checkListMap.has(code)
      })
    },
    // 设置用户数组
    setCargoOwnerOptions(options) {
      this.cargoOwnerArr = options
    },
    // 建议体积
    suggestAllVolume() {
      if (this.computedTotalVolume) {
        this.model.totalVolume = decimalNumFilter(this.computedTotalVolume)
      } else {
        alert('建议体积要是大于0的数字')
      }
    }
  }
}
</script>
